//
//  MainViewController.m
//
//  Created by Sunny on 10-12-2.
//  Copyright 2010 SUNNY. All rights reserved.
//

#import "MainViewController.h"

#import "WZ66WZZBAppDelegate.h"
#import "MagazineViewController.h"
#import "ContentViewController.h"

#import "Magazine.h"
#import "ParseMagazineOperation.h"
#import "HttpMagazineController.h"

#import "Blog.h"
#import "ParseBlogOperation.h"
#import "HttpBlogController.h"

@implementation MainViewController

@synthesize magazineArray;
@synthesize blogArray;

#pragma mark -
#pragma mark HttpInit Method
- (void)httpMethodInit {
	//init
	self.magazineArray = [[[NSMutableArray alloc] init] autorelease];
	[self httpMagazineInit];
}

- (void) httpMagazineInit {
    self.blogArray = [[[NSMutableDictionary alloc] init] autorelease];
}

- (void)httpMagazineXml:(NSString *) magazineXmlUrl {
	//Magazine
	HttpMagazineController *httpMagazineController = [[HttpMagazineController alloc] init];
	[httpMagazineController _startReceive:magazineXmlUrl];
	[httpMagazineController release];
}

- (void)httpBlogXml:(NSString *) blogXmlUrl {
	//Blog
	HttpBlogController *httpBlogController = [[HttpBlogController alloc] init];
	[httpBlogController _startReceive:blogXmlUrl];
	[httpBlogController release];
}

#pragma mark -
#pragma mark Response NSNotification Method
// Our NSNotification callback from the running NSOperation to add the magazine
//
- (void)addMagazines:(NSNotification *)notif {
    assert([NSThread isMainThread]);
    
    [self addMagazinesToList:[[notif userInfo] valueForKey:kMagazineResultsKey]];
}

// The NSOperation "ParseMagazineOperation" calls addMagazines: via NSNotification, on the main thread
//
- (void)addMagazinesToList:(NSArray *)magazines {
	[self.magazineArray addObjectsFromArray:magazines];
	
	MagazineViewController *magazineViewController = [WZ66WZZBAppDelegate sharedAppDelegate].magazineViewController;
	[magazineViewController dataLoad];
	
}

// Our NSNotification callback from the running NSOperation when a parsing error has occurred
//
- (void)magazinesError:(NSNotification *)notif {
    assert([NSThread isMainThread]);
    
    [self handleError:[[notif userInfo] valueForKey:kMagazinesMsgErrorKey]];
}


// Our NSNotification callback from the running NSOperation to add the blog
//
- (void)addBlogs:(NSNotification *)notif {
    assert([NSThread isMainThread]);
    
    [self addBlogsToList:[[notif userInfo] valueForKey:kBlogResultsKey]];
}

// The NSOperation "ParseBlogOperation" calls addBlogs: via NSNotification, on the main thread
//
- (void)addBlogsToList:(NSArray *)blogs {
    Blog *blog = (Blog*)[blogs objectAtIndex:0];
    NSString *magazineKey = blog.magazineKey;
	[self.blogArray setObject:blogs forKey:magazineKey];
    
    [self performSelector:@selector(refreshView) withObject:nil afterDelay:3];
}

- (void) refreshView {
	ContentViewController *contentViewController = [WZ66WZZBAppDelegate sharedAppDelegate].contentViewController;
    [contentViewController.activityIndicator stopAnimating];
	[contentViewController dataLoad];
}

// Our NSNotification callback from the running NSOperation when a parsing error has occurred
//
- (void)blogsError:(NSNotification *)notif {
    assert([NSThread isMainThread]);
    
    [self handleError:[[notif userInfo] valueForKey:kBlogsMsgErrorKey]];
}

// Handle errors in the download by showing an alert to the user. This is a very
// simple way of handling the error, partly because this application does not have any offline
// functionality for the user. Most real applications should handle the error in a less obtrusive
// way and provide offline functionality to the user.
//
- (void)handleError:(NSError *)error {
    NSString *errorMessage = [error localizedDescription];
    UIAlertView *alertView =
    [[UIAlertView alloc] initWithTitle:
     NSLocalizedString(@"Error Title",
                       @"Title for alert displayed when download or parse error occurs.")
                               message:errorMessage
                              delegate:nil
                     cancelButtonTitle:@"OK"
                     otherButtonTitles:nil];
    [alertView show];
    [alertView release];
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if((interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (interfaceOrientation == UIDeviceOrientationLandscapeRight)){
		return YES;
	}
	else {
		return NO;
	}
}

@end
